package com.zxy.ziems.server.device.controller;

import com.zxy.btp.security.annotation.IgnoreLogin;
import com.zxy.ziems.common.pojo.collector.Config;
import com.zxy.ziems.server.device.pojo.dto.CollectorDTO;
import com.zxy.ziems.server.device.pojo.vo.CollectorVO;
import com.zxy.ziems.server.device.service.CollectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采集管理-采集器控制
 * @author 10348359
 * @create 2024-03-25 14:15
 */
@RestController
@Validated
@RequestMapping("/api")
public class CollectorController {
    /**
     * 采集器上报
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/main/data/collector/upload")
    public void add(@Validated(value = CollectorDTO.Add.class) @RequestBody CollectorDTO dto) throws Exception {
        service.upload(dto);
    }

    /**
     * 采集器更新
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/main/data/collector/update")
    public void update(@Validated(value = CollectorDTO.Update.class) @RequestBody CollectorDTO dto) throws Exception {
        service.update(dto);
    }

    /**
     * 删除采集器
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/main/data/collector/delete")
    public void delete(@Validated(value = CollectorDTO.Delete.class) @RequestBody CollectorDTO dto) throws Exception {
        service.deleteById(dto.getId());
    }

    /**
     * 采集器列表
     * @return
     */
    @GetMapping("/main/data/collector/list")
    public List<CollectorVO> list() throws Exception {
        return service.viewList();
    }

    /**
     * 采集器配置下发
     * @return
     */
    @PostMapping("/main/data/collector/config/download")
    public void download(@Validated(value = CollectorDTO.Delete.class) @RequestBody CollectorDTO dto) throws Exception {
        service.downloadConfigurationById(dto.getId());
    }

    /**
     * 根据采集器编码 查询配置
     * @param code 采集器code
     * @return
     */
    @IgnoreLogin
    @GetMapping("/main/data/collector/config")
    public Config download(String code) throws Exception {
        return service.getConfigByCollectorCode(code);
    }


    private CollectorService service;

    @Autowired
    public void setService(CollectorService collectorService) {
        this.service = collectorService;
    }

}
